package com.course.business.controller.admin;/**
 * @author: lin
 */


import com.course.server.dto.*;
import com.course.server.service.CourseCategoryService;
import com.course.server.service.CourseService;
import com.course.server.util.ValidatorUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 *@Author lin
 *@Data Created in 2020/7/28 16:04
 *Description 测试类
 *@Modified By
 *@Version V1.0
 */
@RestController
@RequestMapping("/admin/course")
public class CourseController {
    private static final Logger LOG = LoggerFactory.getLogger(CourseController.class);
    public static final String BUSINESS_NAME = "课程";
    @Resource
    private CourseService courseService;
    @Resource
    private CourseCategoryService courseCategoryService;
    @PostMapping("/list")
    /**
     * create by: lin
     * description: 列表查询
     * create time: 2020/8/2 17:39
     */
    public ResponseDto list(@RequestBody PageDto pageDto){
        ResponseDto responseDto = new ResponseDto();
        courseService.list(pageDto);
        responseDto.setContent(pageDto);
        return responseDto;
    }
    /**
     * create by: lin
     * description: 保存 id有值时更新，无值时新增
     * create time: 2020/8/2 17:40
     */
    @PostMapping("/save")
    public ResponseDto save(@RequestBody CourseDto courseDto){
        //保存校验
            ValidatorUtil.require(courseDto.getName(), "名称");
            ValidatorUtil.length(courseDto.getName(), "名称", 1, 50);
            ValidatorUtil.length(courseDto.getSummary(), "概述", 1, 2000);
            ValidatorUtil.length(courseDto.getImage(), "封面", 1, 100);
        ResponseDto responseDto = new ResponseDto();
        courseService.save(courseDto);
        return responseDto;
    }
    /**
     * create by: lin
     * description: 删除
     * create time: 2020/8/2 17:58
     */
    @DeleteMapping("/delete/{id}")
    public ResponseDto delete(@PathVariable String id){
        ResponseDto responseDto = new ResponseDto();
        courseService.delete(id);
        return responseDto;
    }
    /**
     * create by: lin
     * description: 查找课程所有的分类
     * create time: 2020/8/2 17:58
     */
    @PostMapping("/list-category/{courseId}")
    public ResponseDto listCategory(@PathVariable(value = "courseId") String courseId){
        ResponseDto responseDto = new ResponseDto();
        List<CourseCategoryDto> dtoList = courseCategoryService.listByCourse(courseId);
        responseDto.setContent(dtoList);
        return responseDto;
    }
    /**
     * create by: lin
     * description: 查找课程内容
     * create time: 2020/8/2 17:58
     */
    @GetMapping("/find-content/{id}")
    public ResponseDto findContent(@PathVariable String id){
        ResponseDto responseDto = new ResponseDto();
        CourseContentDto contentDto = courseService.findContent(id);
        responseDto.setContent(contentDto);
        return responseDto;
    }
    /**
     * create by: lin
     * description: 保存课程内容，包含新增和修改
     * create time: 2020/8/2 17:58
     */
    @PostMapping("/save-content")
    public ResponseDto saveContent(@RequestBody CourseContentDto contentDto){
        ResponseDto responseDto = new ResponseDto();
        courseService.saveContent(contentDto);
        responseDto.setContent(contentDto);
        return responseDto;
    }
    /**
     * create by: lin
     * description: 更新排序
     * create time: 2020/8/2 17:58
     */
    @RequestMapping(value = "/sort")
    public ResponseDto sort(@RequestBody SortDto sortDto){
        LOG.info("更新排序");
        ResponseDto responseDto = new ResponseDto();
        courseService.sort(sortDto);
        return responseDto;
    }
}
